//
//  ShareAppVC.m
//  Cunli
//
//  Created by ZhanyaaLi on 15/10/29.
//  Copyright © 2015年 lilubin. All rights reserved.
//

#import "ShareAppVC.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import <MessageUI/MessageUI.h>
#import "Config.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
//#define APP_SHARE_URL @"哇，我刚刚加入了村里，有村内的普法和社交，可以看到家乡里的风土人情和家长里短，注册还免费送手机流量。赶快来下载吧:http://121.41.117.249/wap/app_ewm.php"
#define APP_SHARE_URL @"哇，我刚刚加入了村里，有村内的普法和社交，可以看到家乡里的风土人情和家长里短，注册还免费送手机流量。赶快来下载吧:http://cunli.zhanyaa.com/erweima"

NSString * const appUrl = @"http://cunli.zhanyaa.com/erweima";

@interface ShareAppVC () <MFMessageComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *messageImg;
@property (weak, nonatomic) IBOutlet UIImageView *qqImg;
@property (weak, nonatomic) IBOutlet UIImageView *wechatImg;
@property (weak, nonatomic) IBOutlet UIButton *messageBtn;
@property (weak, nonatomic) IBOutlet UIButton *qqBtn;
@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;

@end

@implementation ShareAppVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _messageImg.userInteractionEnabled = YES;
    _qqImg.userInteractionEnabled = YES;
    _wechatImg.userInteractionEnabled = YES;
    self.title = @"分享村里";
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToMessage)];
    tap1.numberOfTapsRequired = 1;
    tap1.numberOfTouchesRequired = 1;
    [_messageImg addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToQQ)];
    tap2.numberOfTapsRequired = 1;
    tap2.numberOfTouchesRequired = 1;
    [_qqImg addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToWechat)];
    tap3.numberOfTapsRequired = 1;
    tap3.numberOfTouchesRequired = 1;
    [_wechatImg addGestureRecognizer:tap3];
    
    [_messageBtn addTarget:self action:@selector(shareToMessage) forControlEvents:UIControlEventTouchUpInside];
    [_qqBtn addTarget:self action:@selector(shareToQQ) forControlEvents:UIControlEventTouchUpInside];
    [_wechatBtn addTarget:self action:@selector(shareToWechat) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view setBackgroundColor:[UIColor colorWithHexString:UIBgColorValue]];
    [_firstLabel setTextColor:[UIColor colorWithHexString:InfoCellTitleColorValue]];
    [_secondLabel setTextColor:[UIColor colorWithHexString:InfoCellTitleColorValue]];
    UIColor *btnColor = [UIColor colorWithHexString:InfoCellIntroduceColorValue];
    [_qqBtn setTitleColor:btnColor forState:UIControlStateNormal];
    [_wechatBtn setTitleColor:btnColor forState:UIControlStateNormal];
    [_messageBtn setTitleColor:btnColor forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)shareToMessage
{
    NSLog(@"Share Message");
    [self showMessageView:nil title:@"村里下载地址: " body:APP_SHARE_URL];
}

- (void)shareToQQ
{
    NSLog(@"Share QQ");
    [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
    [UMSocialData defaultData].extConfig.qqData.title = @"村里APP下载";
    [UMSocialData defaultData].extConfig.qqData.url = appUrl;
    [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ] content:APP_SHARE_URL image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                        NSLog(@"QQ分享D成功");
        if (response.responseCode == UMSResponseCodeSuccess)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
//                [weakSelf postPointGetRequestWithString:@"4"];
            });
        }
    }];
    

}

- (void)shareToWechat
{
    NSLog(@"Share Wechat");
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = appUrl;
    [UMSocialData defaultData].extConfig.title = @"村里APP下载";
    
    [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:APP_SHARE_URL image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                        NSLog(@"分享成功");
        dispatch_async(dispatch_get_main_queue(), ^{
//            [self postPointGetRequestWithString:@"1"];
        });
    }];
}

- (void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
        [[messageVC navigationBar] setTintColor:[UIColor whiteColor]];
        [messageVC setNavigationBarHidden:YES];
        messageVC.recipients = phones;
        messageVC.body = body;
        messageVC.messageComposeDelegate = self;
        [[[[messageVC viewControllers] lastObject] navigationItem] setTitle:title];
        
        UIViewController *curVC = [[messageVC viewControllers] lastObject];
        
        if (curVC.navigationController != nil) {
            curVC.fd_prefersNavigationBarHidden = YES;
        }
        
        [self presentViewController:messageVC animated:YES completion:NULL];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"该设备不支持短信功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

#pragma mark - MFMessageDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:NULL];
//    [self.navigationController.navigationBar setHidden:NO];
    switch (result) {
        case MessageComposeResultSent:
        {
            NSLog(@"信息发送成功");
            [Config showHUDWithText:@"信息发送成功" andImage:@"done"];
            break;
        }
        case MessageComposeResultFailed:
        {
            NSLog(@"信息发送失败");
            [Config showHUDWithText:@"信息发送失败" andImage:@"error"];
            break;
        }
        case  MessageComposeResultCancelled:
        {
//            NSLog(@"用户取消发送信息");
//            [Config showHUDWithText:@"用户取消发送信息" andImage:@"error"];
            break;
        }
        default:
            break;
    }
}

@end
